package com.yyq.gxdc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.gxdc.bean.Result;
import com.yyq.gxdc.entity.DiningTable;
import com.yyq.gxdc.entity.Dish;
import com.yyq.gxdc.entity.ReceivingAddress;
import com.yyq.gxdc.entity.User;
import com.yyq.gxdc.service.IDiningTableService;
import com.yyq.gxdc.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/diningTable")
public class DiningTableController {

    @Autowired
    private IDiningTableService diningTableService;

    /**
     * 后台分页查找
     * @param name
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @GetMapping("/mgr/diningTableList")
    @ResponseBody
    public Result<IPage<DiningTable>> mgrDiningTableList(
            String name,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Model model) {

        Result<IPage<DiningTable>> result = new Result<IPage<DiningTable>>();
        Page<DiningTable> page = new Page<DiningTable>(pageNum, pageSize);
        DiningTable diningTable = new DiningTable();
        diningTable.setName(name);
        IPage<DiningTable> allRoute = diningTableService.getAllDiningTable(page, diningTable);
        result.setSuccess(true);
        result.setResult(allRoute);
        return result;
    }

    /**
     * 查找未使用餐台 -- diningTableList
     * @return
     */
    @GetMapping("/front/getUnUseTableList")
    @ResponseBody
    public Result<List<DiningTable>> getUnUseTableList(
            HttpServletRequest request,Integer tableType) {

        Result<List<DiningTable>> result = new Result<List<DiningTable>>();
        List<DiningTable> unUseTableList = diningTableService.getUnUseTable(tableType);
        result.setSuccess(true);
        result.setResult(unUseTableList);
        return result;
    }

    /**
     * 详情
     * @param rid
     * @return
     */
    @GetMapping("/getDishDetail")
    @ResponseBody
    public Result<DiningTable> getRouteDetail(int rid, HttpServletRequest req) {
        Result<DiningTable> result = new Result<>();
        DiningTable detailByRid = diningTableService.getDetailByRid(rid);
        result.setResult(detailByRid);
        result.setSuccess(true);
        return result;
    }


    /**
     * 添加菜品管理信息
     * @param diningTable
     * @param req
     * @return
     */
    @PostMapping(value = "/addDiningTable")
    @ResponseBody
    public Result<Object> addUser(DiningTable diningTable, HttpServletRequest req) {
        // 新增 餐台管理
        diningTableService.add(diningTable);
        return Result.ok();
    }

    /**
     * 删除菜品
     * @param id
     * @param req
     * @return
     */
    @PostMapping(value = "/deleteDiningTable")
    @ResponseBody
    public Result<Object> deleteDiningTable(int id, HttpServletRequest req) {
        // 先查询 这个餐台 是否已存在
        DiningTable byId = diningTableService.findById(id);
        if(byId == null ){
            return Result.error("该餐台不存在！");
        }else{
            diningTableService.delete(id);
            return Result.ok();
        }
    }

    /**
     * 修改餐台
     * @param diningTable
     * @param req
     * @return
     */
    @PostMapping(value = "/updateDiningTable")
    @ResponseBody
    public Result<Object> updateDiningTable(DiningTable diningTable, HttpServletRequest req) {
        // 先查询 这个 行程 是否已存在
        DiningTable byId = diningTableService.findById(diningTable.getId());
        if(byId == null ){
            return Result.error("该餐台不存在！");
        }else{
            diningTableService.update(diningTable);
        }
            return Result.ok();
    }

    /**
     * 编辑详情
     * @param id
     * @return
     */
    @GetMapping("/getDetailByRid")
    @ResponseBody
    public Result<DiningTable> getDetailByRid(Integer id, HttpServletRequest req) {
        Result<DiningTable> result = new Result<>();
        // 先查询 这个餐台是否已存在
        DiningTable byId = diningTableService.findById(id);
        if(byId == null ){
            result.setSuccess(false);
            result.setMessage("该菜品不存在！");
            return result;
        }else{
            result.setResult(byId);
            result.setSuccess(true);
            return result;
        }
    }
}
